package v1

import (
	"gin_blog/pkg/app"
	"gin_blog/pkg/e"
	"gin_blog/pkg/setting"
	"gin_blog/pkg/util"
	"gin_blog/service/tag_service"
	"github.com/astaxie/beego/validation"
	"github.com/gin-gonic/gin"
	"github.com/unknwon/com"
	"net/http"
)

//@Tags 标签路由
// @Summary 获取多个文章标签
// @Produce  json
// @Param name query string false "Name"
// @Param page query int false "Page"
// @Param state query int false "State"
// @Success 200 {string} json "{
//    "code": 200,
//    "data": {
//        "lists": [
//            {
//                "id": 3,
//                "created_on": 0,
//                "modified_on": 0,
//                "name": "html",
//                "created_by": "黄喆",
//                "modified_by": "",
//                "state": 1
//            }
//        ],
//        "total": 1
//    },
//    "msg": "ok"
//}"
// @Router /api/v1/tags [get]
func GetTags(c *gin.Context) {
	appG := app.Gin{C: c}
	valid := validation.Validation{}
	name := c.Query("name")

	var state int = -1
	if arg := c.Query("state"); arg != "" {
		state = com.StrTo(arg).MustInt()
		valid.Range(state, 0, 1, "state")
	}
	if valid.HasErrors() {
		app.MarkErrors(valid.Errors)
		appG.Response(http.StatusBadRequest, e.INVALID_PARAMS, nil)
		return
	}
	tagService := tag_service.Tag{
		Name:     name,
		State:    state,
		PageNum:  util.GetPage(c),
		PageSize: setting.AppSetting.PageSize,
	}
	total, err := tagService.Count()
	if err != nil {
		appG.Response(http.StatusInternalServerError, e.ERROR_COUNT_TAG_FAIL, nil)
		return
	}
	tags, err := tagService.Gets()
	if err != nil {
		appG.Response(http.StatusInternalServerError, e.ERROR_GET_TAGS_FAIL, nil)
		return
	}
	data := make(map[string]interface{})
	data["lists"] = tags
	data["total"] = total
	appG.Response(http.StatusOK, e.SUCCESS, data)
}

type AddTagForm struct {
	Name      string `json:"name" form:"name" valid:"Required;MaxSize(20)"`
	CreatedBy string `json:"created_by" form:"created_by" valid:"MaxSize(20)"`
	State     int    `json:"state" form:"state" valid:"Range(0,1)"`
}

//@Tags 标签路由
// @Summary 新增文章标签
// @Produce  json
// @Param name query string true "Name"
// @Param state query int false "State"
// @Param created_by query int false "CreatedBy"
// @Success 200 {string} json "{"code":200,"data":{},"msg":"ok"}"
// @Router /api/v1/tags [post]
func AddTag(c *gin.Context) {
	var (
		appG = app.Gin{C: c}
		form AddTagForm
	)
	httpCode, errCode := app.BindAndValid(c, &form)
	if errCode != e.SUCCESS {
		appG.Response(httpCode, errCode, nil)
	}
	tagService := tag_service.Tag{
		Name:      form.Name,
		CreatedBy: form.CreatedBy,
		State:     form.State,
	}
	exists, err := tagService.ExistByName()
	if err != nil {
		appG.Response(http.StatusInternalServerError, e.ERROR_EXIST_TAG_FAIL, nil)
		return
	}
	if exists {
		appG.Response(http.StatusOK, e.ERROR_EXIST_TAG, nil)
		return
	}
	if err = tagService.Add(); err != nil {
		appG.Response(http.StatusInternalServerError, e.ERROR_ADD_TAG_FAIL, nil)
		return
	}
	appG.Response(http.StatusOK, e.SUCCESS, nil)
}

type EditTagForm struct {
	ID         int    `json:"id" form:"id" valid:"Required;Min(1)"`
	Name       string `json:"name" form:"name" valid:"Required;MaxSize(20)"`
	ModifiedBy string `json:"modified_by" form:"modified_by" valid:"MaxSize(20)"`
	State      int    `json:"state" form:"state" valid:"Range(0,1)"`
}

//@Tags 标签路由
// @Summary 修改文章标签
// @Produce  json
// @Param id path int true "ID"
// @Param name query string true "ID"
// @Param state query int false "State"
// @Param modified_by query string true "ModifiedBy"
// @Success 200 {string} json "{"code":200,"data":{},"msg":"ok"}"
// @Router /api/v1/tags/{id} [put]
func EditTag(c *gin.Context) {
	var (
		appG = app.Gin{C: c}
		form= EditTagForm{ID: com.StrTo(c.Param("id")).MustInt()}
	)
	httpCode, errCode := app.BindAndValid(c, &form)
	if errCode != e.SUCCESS {
		appG.Response(httpCode, errCode, nil)
		return
	}
	tagService := tag_service.Tag{
		ID:         form.ID,
		Name:       form.Name,
		ModifiedBy: form.ModifiedBy,
		State:      form.State,
	}
	exists, err := tagService.ExistByID()
	if err != nil {
		appG.Response(http.StatusInternalServerError, e.ERROR_EXIST_TAG_FAIL, nil)
		return
	}
	if !exists {
		appG.Response(http.StatusOK, e.ERROR_NOT_EXIST_TAG, nil)
		return
	}
	err = tagService.Edit()
	if err != nil {
		appG.Response(http.StatusInternalServerError, e.ERROR_EDIT_TAG_FAIL, nil)
		return
	}
	appG.Response(http.StatusOK, e.SUCCESS, nil)
}

//@Tags 标签路由
// @Summary 删除文章标签
// @Produce  json
// @Param id path int true "ID"
// @Success 200 {string} json "{"code":200,"data":{},"msg":"ok"}"
// @Router /api/v1/tags/{id} [delete]
func DeleteTag(c *gin.Context) {
	appG := app.Gin{C: c}
	id := com.StrTo(c.Param("id")).MustInt()

	valid := validation.Validation{}
	valid.Min(id, 1, "id").Message("id必须大于0")
	tagService :=tag_service.Tag{ID: id}
	exists,err :=tagService.ExistByID()
	if err!=nil{
		appG.Response(http.StatusInternalServerError,e.ERROR_EXIST_TAG_FAIL,nil)
		return
	}
	if !exists{
		appG.Response(http.StatusOK,e.ERROR_NOT_EXIST_TAG,nil)
		return
	}
	err =tagService.Delete()
	if err!=nil{
		appG.Response(http.StatusInternalServerError,e.ERROR_DELETE_TAG_FAIL,nil)
		return
	}
	appG.Response(http.StatusOK,e.SUCCESS,nil)
}
